package com.youkeyi.ddy.cloud.application.business.crm.vaccination.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.BusinessVaccinationDetailsCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.dto.BusinessVaccinationDetailsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.dto.BusinessVaccinationDetailsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.param.BusinessVaccinationDetailsCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.param.BusinessVaccinationDetailsDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.param.BusinessVaccinationDetailsListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.vaccination.param.BusinessVaccinationDetailsUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.CrmVaccinationDetailsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.dto.CrmVaccinationDetailsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.vaccination.param.CrmVaccinationDetailsUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessVaccinationDetailsController
 *
 * @Author liuxiawang
 * @Date 2024/4/3 11:17 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessVaccinationDetailsController implements BusinessVaccinationDetailsCloudService {
    
    @Autowired
    private CrmVaccinationDetailsCloudService crmVaccinationDetailsCloudService;

    @Override
    public BusinessVaccinationDetailsListCloudDto list(BusinessVaccinationDetailsListCloudParam param) {
        BusinessVaccinationDetailsListCloudDto dto = new BusinessVaccinationDetailsListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmVaccinationDetailsQueryCloudParam vaccinationDetailsQueryCloudParam = new CrmVaccinationDetailsQueryCloudParam();
        vaccinationDetailsQueryCloudParam.setVaccinationDetailsPrimaryKeys(param.getVaccinationDetailsPrimaryKeys());
        vaccinationDetailsQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        vaccinationDetailsQueryCloudParam.setOrderKeys(param.getOrderKeys());
        vaccinationDetailsQueryCloudParam.setGoodsKeys(param.getGoodsKeys());
        vaccinationDetailsQueryCloudParam.setPageSize(param.getPageSize());
        vaccinationDetailsQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmVaccinationDetailsListCloudDto vaccinationDetailsDtos = crmVaccinationDetailsCloudService.query(vaccinationDetailsQueryCloudParam);
        if (null != vaccinationDetailsDtos && CollectionUtils.isNotEmpty(vaccinationDetailsDtos.getRows())) {
            List<BusinessVaccinationDetailsListItemCloudDto> items = new ArrayList<>();
            vaccinationDetailsDtos.getRows().forEach(vaccinationDetails -> {
                BusinessVaccinationDetailsListItemCloudDto item = new BusinessVaccinationDetailsListItemCloudDto();
                item.setVaccinationDetailsPrimaryKey(vaccinationDetails.getVaccinationDetailsPrimaryKey());
                item.setCorpKey(vaccinationDetails.getCorpKey());
                item.setOrderKey(vaccinationDetails.getOrderKey());
                item.setGoodsKey(vaccinationDetails.getGoodsKey());
                item.setGoodsName(vaccinationDetails.getGoodsName());
                item.setVaccineDoses(vaccinationDetails.getVaccineDoses());
                item.setNumberOfStitches(vaccinationDetails.getNumberOfStitches());
                item.setDoctorKey(vaccinationDetails.getDoctorKey());
                item.setDoctorName(vaccinationDetails.getDoctorName());
                item.setRemark(vaccinationDetails.getRemark());
                item.setStatus(vaccinationDetails.getStatus());
                item.setIsDeleted(vaccinationDetails.getIsDeleted());
                item.setIsDeletedName(vaccinationDetails.getIsDeletedName());
                item.setCreateUserKey(vaccinationDetails.getCreateUserKey());
                item.setCreateUserName(vaccinationDetails.getCreateUserName());
                item.setCreateTime(vaccinationDetails.getCreateTime());
                item.setUpdateUserKey(vaccinationDetails.getUpdateUserKey());
                item.setUpdateUserName(vaccinationDetails.getUpdateUserName());
                item.setUpdateTime(vaccinationDetails.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(vaccinationDetailsDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessVaccinationDetailsCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getOrderKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmVaccinationDetailsCreateCloudParam vaccinationDetailsCreateCloudParam = new CrmVaccinationDetailsCreateCloudParam();
        vaccinationDetailsCreateCloudParam.setCorpKey(corpKey);
        vaccinationDetailsCreateCloudParam.setOrderKey(param.getOrderKey());
        vaccinationDetailsCreateCloudParam.setGoodsKey(param.getGoodsKey());
        vaccinationDetailsCreateCloudParam.setGoodsName(param.getGoodsName());
        vaccinationDetailsCreateCloudParam.setVaccineDoses(param.getVaccineDoses());
        vaccinationDetailsCreateCloudParam.setNumberOfStitches(param.getNumberOfStitches());
        vaccinationDetailsCreateCloudParam.setDoctorKey(param.getDoctorKey());
        vaccinationDetailsCreateCloudParam.setDoctorName(param.getDoctorName());
        vaccinationDetailsCreateCloudParam.setRemark(param.getRemark());
        vaccinationDetailsCreateCloudParam.setStatus(param.getStatus());
        vaccinationDetailsCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccinationDetailsCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmVaccinationDetailsCloudService.create(vaccinationDetailsCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessVaccinationDetailsUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getVaccinationDetailsPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmVaccinationDetailsUpdateCloudParam vaccinationDetailsUpdateCloudParam = new CrmVaccinationDetailsUpdateCloudParam();
        vaccinationDetailsUpdateCloudParam.setVaccinationDetailsPrimaryKey(param.getVaccinationDetailsPrimaryKey());
        vaccinationDetailsUpdateCloudParam.setVaccineDoses(param.getVaccineDoses());
        vaccinationDetailsUpdateCloudParam.setNumberOfStitches(param.getNumberOfStitches());
        vaccinationDetailsUpdateCloudParam.setDoctorKey(param.getDoctorKey());
        vaccinationDetailsUpdateCloudParam.setDoctorName(param.getDoctorName());
        vaccinationDetailsUpdateCloudParam.setRemark(param.getRemark());
        vaccinationDetailsUpdateCloudParam.setStatus(param.getStatus());
        vaccinationDetailsUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccinationDetailsUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmVaccinationDetailsCloudService.update(vaccinationDetailsUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessVaccinationDetailsDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getVaccinationDetailsPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmVaccinationDetailsDeleteCloudParam vaccinationDetailsDeleteCloudParam = new CrmVaccinationDetailsDeleteCloudParam();
        vaccinationDetailsDeleteCloudParam.setVaccinationDetailsPrimaryKeys(param.getVaccinationDetailsPrimaryKeys());
        vaccinationDetailsDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        vaccinationDetailsDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmVaccinationDetailsCloudService.delete(vaccinationDetailsDeleteCloudParam);
    }
}
